Reverse Linked List II
Medium
Question
Given the head of a linked list and two integers left
and right
, reverse the nodes of the list from index left
(inclusive) to index right
(exclusive), and return the head of the list.
Note: The head of the linked list is at index 0.
Input: head = [4 -> 7 -> 6 -> 5 -> 1], left = 1, right = 4
Output: [4 -> 5 -> 6 -> 7 -> 1]
Input: head = [0 -> 1 -> 2 -> 3], left = 2, right = 3
Output: [0 -> 1 -> 2 -> 3]
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
What is the correct output if the following is our input? head = [0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6], left = 3, right = 6
[0 -> 1 -> 2 -> 5 -> 4 -> 3 -> 6]
[0 -> 1 -> 2 -> 3 -> 5 -> 4 -> 6]
[0 -> 1 -> 5 -> 4 -> 3 -> 2 -> 6]
[0 -> 1 -> 2 -> 6 -> 5 -> 4 -> 3]
Take a moment to understand the problem and think of your approach before you start coding.